%==========================================================================
%=  This file is part of the Sound Restoration Project
%=  (c) Copyright Industrial Mathematics Institute
%=                University of South Carolina, Department of Mathematics
%=  ALL RIGHTS RESERVED
%=
%=  Author: Borislav Karaivanov
%==========================================================================

%==========================================================================
% List of the files on which this procedure depends:
%
% read2DArrayFromFile.m
% setLogWritingFunctHandle.m
% writeToLogFile.m
%
%==========================================================================

%==========================================================================
% The function "write2DArrayToFile" writes a 2D array of floats to a
% specified text file, column after column.
% INPUT: "fileName" is a string specifying the name of the text file to be
% written including a full directory path.
% "write2DArr" is a 2D array to be written to a text file.
% "writeToLogFunctHandle" is an optional function handle which writes to a
% fixed log text file if keeping a log is requested, or does nothing
% otherwise.
% OUTPUT: None.
%==========================================================================
function write2DArrayToFile(fileName, write2DArr, writeToLogFunctHandle)

if (nargin < 3)
    writeToLogFunctHandle = @(x) '';
end

% Keep repeating the writing process until the data read from the output
% file agrees with the given data but no more than "numAttempts" times.
numAttempts = 5;
for currAttempt = 1:numAttempts
    
    if (currAttempt > 1)
        writeToLogFunctHandle(['Warning in write2DArrayToFile: Starting attempt ' ...
            num2str(currAttempt) ' of ' ...
            num2str(numAttempts) ' in writing a 2D array to a text file.']);
    end
    
    % Indicate that the current attempt at writing has not failed yet.
    checkFailed = false;
    
    % Open the text file for writing.
    fid = fopen(fileName, 'wt');
    if (fid == -1)
        writeToLogFunctHandle(['Warning in write2DArrayToFile: Unable to open the file ' ...
            fileName ' for writing.']);
        return;
    end
    
    % Write the 2D array, column after column.
    fprintf(fid, 'number of rows: %d\n', size(write2DArr, 1));
    fprintf(fid, 'number of columns: %d\n', size(write2DArr, 2));
    fprintf(fid, '%.10f\n', write2DArr);
    
    % Close the file.
    fclose(fid);
    
    % Check the data just written by reading in the text file and comparing
    % to the given data.
    read2DArr = read2DArrayFromFile(fileName, writeToLogFunctHandle);
    
    % Compare the read data to the written data.
    if ( (numel(size(read2DArr)) == numel(size(write2DArr)))&& ...
            (nnz(size(read2DArr) ~= size(write2DArr)) == 0)&& ...
            (nnz(abs(read2DArr(:) - write2DArr(:)) > 1.0e-010) == 0) )
        break;
    else
        checkFailed = true;
    end
end

if (checkFailed == true)
    writeToLogFunctHandle(['Warning in write2DArrayToFile: Failed to write the file ' ...
        fileName ' in ' num2str(numAttempts) ' attempts.']);
end

return;
% end of the function "write2DArrayToFile"
